<?php
namespace Home\Controller;
class UserController extends CommonController{

	/* 检查用户是否已注册 */
	public function checkUserByPhone(){
		$mustData = array(
			'phone'=>$_POST['phone'],
		);
		CheckMustData($mustData);
		$userInfo = M('User')->where(array('us_phone'=>$_POST['phone']))->find();
		if(empty($userInfo)){
			ApiResult(306);
		}else{
			ApiResult(200,$userInfo);
		}
	}
	/*设置用户昵称*/
	public function setNickName(){
		$mustData = array(
			'userId'=>$_POST['userId'],
			'nickname'=>$_POST['nickname'],
		);
		CheckMustData($mustData);
		$userInfo = M('User')->where(array('us_id'=>$_POST['userId']))->find();
		if(empty($userInfo)){
			ApiResult(306);
		}else{
			if(mb_strlen($_POST['nickname'],'utf8')<2){
			}else{
				M('User')->where(array('us_id'=>$_POST['userId']))->save(array('us_nickname'=>htmlspecialchars($_POST['nickname'])));
			}
			ApiResult(200);
		}
	}
	/* 获取用户信息 */
	public function getUserInfo(){
		$mustData = array(
			'userId'=>$_POST['userId'],
		);
		CheckMustData($mustData);
		$userInfo = M('User')->where(array('us_id'=>$_POST['userId']))->find();

		$sysconfig = C('sysconfig');
		$condition['sc_id'] = $sysconfig['other'];
		$ruleData = M("SystemConfig")->where($condition)->find();
		$awardRule = "邀请好友注册时填写邀请码，好友前".$ruleData['sc_value']."次充值时，您都可以获得".$ruleData['sc_value_2']."% 的奖励，超过30元可以联系客服兑换现金，10奖励币=1元。可邀请多人，奖励累计！";
		$userInfo['awardRuleMsg']=$awardRule;
		if(empty($userInfo)){
			ApiResult(306);
		}else{
			ApiResult(200,$userInfo);
		}
	}

	/* 获取奖励记录 */
	public function getAwardLog(){
		$mustData = array(
			'userId'=>$_POST['userId'],
		);
		CheckMustData($mustData);

		$limit = I("pageSize", 30, "intval");
        $page = I("pageCurrent", 1, "intval");
        $awardLog_model = M("AwardLog");
        $condition['aw_target'] = $_POST['userId'];
        $condition['aw_type'] = 0;
        $count = $awardLog_model->where($condition)->count();
        $dataList = $awardLog_model->where($condition)->limit($limit)->page($page)->order("aw_id desc")->select();
        foreach ($dataList as $key => $value) {
        	if((int)$value['aw_source']!=0){
        		$userData = M('User')->where(array('us_id'=>$value['aw_source']))->find();
        		$dataList[$key]['aw_source_phone'] = substr($userData['us_phone'],0,8)."***";
        	}else{
        		$dataList[$key]['aw_source_phone'] = "系统充值***";
        	}
        	$dataList[$key]['aw_create_time'] = date('Y-m-d H:i:s',$value['aw_create_time']);
        	$dataList[$key]['aw_target'] = $value['aw_value'];
        }
        $resultData = array(
        	'count'=>$count,
        	'dataList'=>$dataList
        );
        ApiResult(200,$resultData);
	}
	/* 充值卡充值 */
	public function recharge(){
		$mustData = array(
			'userId'=>$_POST['userId'],
			'pc_number'=>strtoupper($_POST['cradNumber'])
		);
		CheckMustData($mustData);
		$mustData['pc_number'] = str_replace(" ","",$mustData['pc_number']);
		/* 充值卡检测 */
		$cardData = M('PrepaidCard')->where(array('pc_number'=>$mustData['pc_number']))->find();
		if(empty($cardData)||$cardData['pc_is_use']==1){
			ApiResult(307);
		}

		/* 用户检测 */
		$userData = M('User')->where(array('us_id'=>$mustData['userId']))->find();
		if(empty($userData)){
			ApiResult(306);
		}
		$model = M();
        $model->startTrans();

		$cardSaveData = array(
			'pc_is_use'=>1,
			'pc_use_time'=>time(),
			'pc_use_user'=>$mustData['userId'],
		);
		$card_result = $model->table(C('DB_PREFIX').'prepaid_card')->where(array('pc_number'=>$mustData['pc_number']))->save($cardSaveData);

		$userSaveData = array(
			'us_cloud_money'=>((int)$cardData['pc_value']+(int)$userData['us_cloud_money']),
		);
		if((int)$cardData['pc_value']>=50){
			$awardLogData = array(
				'aw_value'=>((int)$cardData['pc_value']),
				'aw_target'=>$userData['us_id'],
				'aw_source'=>$userData['us_id'],
				'aw_create_time'=>time(),
				'aw_type'=>1
			);
			$model->table(C('DB_PREFIX').'award_log')->add($awardLogData);
		}
		$user_result = $model->table(C('DB_PREFIX').'user')->where(array('us_id'=>$mustData['userId']))->save($userSaveData);

		/* 充值奖励 START */
		if($userData['us_use_invita_code']!=null&&$userData['us_use_invita_code']!=''){
			/*获取奖励币比例信息*/
			$sysconfig = C('sysconfig');
			$condition['sc_id'] = $sysconfig['other'];
			$ruleData = M("SystemConfig")->where($condition)->find();
			$rule = ($ruleData['sc_value_2']!=null&&$ruleData['sc_value_2']!='')?(int)$ruleData['sc_value_2']:0;
			/* 获取好友信息 */
			$friendData = M('User')->where(array('us_invita_code'=>$userData['us_use_invita_code']))->find();
			$friendSaveData = array(
				'us_award_money'=>((int)$cardData['pc_value']/100*$rule)+(int)$friendData['us_award_money'],
			);

			/* 奖励次数限制 */
			$count_condition = array(
				'aw_target'=>$friendData['us_id'],
				'aw_source'=>$userData['us_id'],
				'aw_type'=>0,
			);
			$count = M("AwardLog")->where($count_condition)->count();
			if($count<intval($ruleData['sc_value'])){
				$model->table(C('DB_PREFIX').'user')->where(array('us_id'=>$friendData['us_id']))->save($friendSaveData);
				$awardLogData = array(
					'aw_value'=>((int)$cardData['pc_value']/100*$rule),
					'aw_target'=>$friendData['us_id'],
					'aw_source'=>$userData['us_id'],
					'aw_create_time'=>time(),
					'aw_type'=>0,
				);
				$model->table(C('DB_PREFIX').'award_log')->add($awardLogData);
			}
		}
		/* 充值奖励 END */

		if($card_result&&$user_result){
			$model->commit();
			ApiResult(200);
		}else{
			$model->rollback();
			ApiResult(300);
		}
	}

}
?>